﻿using commonImportPlugin.Configs.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace commonImportPlugin.Configs.Other
{
    /// <summary>
    /// 数量检查类
    /// </summary>
    public class CountCheckConfig : ConfigsBase
    {
        //
        //    基本配置参数
        //
        #region
        /// <summary>
        /// 检查数量的规则
        /// </summary>
        public string[] countCheck { set; get; }
        /// <summary>
        /// 错误是否继续
        /// </summary>
        public bool differIsContinue { set; get; }
        #endregion

        //
        //    其他
        //
        #region
        private static CountCheckConfig uniqueInstance;
        public void init()
        {
            CountCheckConfig countCheckConfig = new CountCheckConfig();
            //数量检查
            string countRegex = @"^([^\^]+)\^([^\^]+)\^([^\^]+)$";

            if (IsKeyExist("CountChecks"))
            {
                string[] countChecks = ToStringArrays(configs["CountChecks"]);
                if (countChecks != null)
                {
                    foreach (string countCheck in countChecks)
                    {
                        if (!Regex.IsMatch(countCheck, countRegex))
                            throw new Exception(string.Format("\n参数：CountChecks中（检查数量）的[{0}],设置有误，请检查", countCheck));
                    }
                }
                countCheckConfig.countCheck = ToStringArrays(configs["CountChecks"]);
            }

            //数量不一致是否继续
            if (IsKeyExist("DifferIsContinue"))
            {
                if (string.IsNullOrEmpty(configs["DifferIsContinue"]))
                    countCheckConfig.differIsContinue = true;//默认值
                else
                {
                    if (!Regex.IsMatch(configs["DifferIsContinue"], @"^[01]$"))
                        throw new Exception("\n参数：DifferIsContinue（数量不一致是否继续）：值只能为1继续或0报错不往下执行");
                    countCheckConfig.differIsContinue = configs["DifferIsContinue"].Trim() == "1" ? true : false;
                }
            }
            else
                countCheckConfig.differIsContinue = true;

            uniqueInstance = countCheckConfig;

        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static CountCheckConfig GetConfig()
        {
            if (uniqueInstance == null)
            {
                return null;
            }
            return uniqueInstance;
        }
        #endregion
    }
}
